	//------------------
	//调试工具
	//------------------	   
   function print(msg) {      
			if (debug) {
	    	debug_html = msg + "<br/>"+debug_html;
	    	$("#debug_div").css("display","block");
	      $("#debug_div").html(debug_html);
	    }
   }
	   

	//------------------
	//翻译
	//------------------
	String.prototype.t = function (){
	                            var str;
	                            if(oPrefs.getMsg(this)!=""){
	                            str=oPrefs.getMsg(this)
	                            }else{
	                            str=this;
	                            }
	                            return str;
	 }

	//------------------
	//判断气温的颜色
	//------------------
	function tempalert(temp){
		var color="";
		var r=255;
		var g=255;
		var b=145;
		if(temp<0){
			b=15*(0-temp);
			if(b>255){
			  b=255;
			}
		}else{
		   for(var i=0;i<temp;i++){
		     b=b-15;
		   }
		   if(b<0){
		      g=b+255;
		      b=0;
		      if(g<0){
		         r=g+255;
		         g=0;
		         if(r<150){r=150;}
		      }
		   }
		}
		color="rgb("+new String(r)+","+new String(g)+","+new String(b)+")";
		return color;
	 }
	//------------------
	//判断白天黑夜
	//------------------
	function fnTN(str,sunr,suns){
	  var b;
	  var nt=parseFloat(str.replace(/:/, "."));
	  var AP=str.substr(str.length-2);
	  var rt=parseFloat(sunr.replace(/:/, "."));
	  var st=parseFloat(suns.replace(/:/, "."));
	  if(AP=="AM"){
	    if(nt>rt && nt<12){
	    b=true;
	    }else{
	    b=false;
	    }
	  }else{
	    if(nt<st || nt>12){
	    b=true;
	    }else{
	    b=false;
	    }
	  }
	  return b;
	 }

	//------------------
	//查找索引
	//------------------
	function indexof(code){
	         var i;
	             for(i=0;i<aCityCodes.length;i++){
	                if(aCityCodes[i]==code)
	                return i;
	             }
	 }

	//------------------
	//关闭消息框
	//------------------
	function fnCloseMsg(){
	         _gel("msgtext").innerHTML="";
	         _gel('msg').style.display='none';	         
	 }

	//------------------
	//显示消息框
	//------------------
	function fnOpenMsg(html){
	         _gel("msg").style.width = _gel("weatherPanel").offsetWidth;
	         _gel("msgtext").innerHTML=html;
	         _gel("msg").style.display="block";
	 }


	//------------------
	//插入数据到DOM
	//------------------
	function fnSetContent(xml){
	         var data = new fnPartXml(xml);
           print("set content"); 
	         _gelstn("h1")[0].firstChild.nodeValue=data.Ct;
	         _gel("Ctmp").innerHTML=data.Ctmp;
	         if(data.ut!="F"){
	         _gel("temp").style.color=tempalert(data.Ctmp);
	         }else{
	         _gel("temp").style.color=tempalert((data.Ctmp-32)*5/9);
	         }
	         _gel("tempicon").setAttribute("src","http://myweather.googlecode.com/svn/trunk/images/icon/"+data.Cicon+"d.png");
	         _gel("tempicon").setAttribute("alt",data.Ct);
	         
	         //-----面板颜色----
	         if(fnTN(data.tm,data.sunr,data.suns)){
	               _gel("navpanel").className="d";
	               _gel("msg").className="d popupbox";
	               _gel("urlary").className="d";
	               
	         }else{
	               _gel("navpanel").className="n";
	               _gel("msg").className="n popupbox";
	               _gel("urlary").className="n";
	               
	         }

	         ///当前链接
           _gel("nowcity").innerHTML=aCityNames[iCurrenCityId];
           var v = _gel("urlary");
           v.style.display="none";
           v.innerHTML="";

           //load stop
           Loadingend();
           print("set content end!"); 

          //兼容IE6-的PNG图片通透
          try{correctPNG()}catch(err){}
          gadgets.window.adjustHeight($("#navpanel").height());
	        }
	//------------------
	//解析XML数据
	//------------------
	function fnPartXml(xml){
	         print("part xml!");
	         //头信息
	         this.ut = xml.getElementsByTagName("ut")[0].firstChild.nodeValue;
	         this.ud = xml.getElementsByTagName("ud")[0].firstChild.nodeValue;
	         this.us = xml.getElementsByTagName("us")[0].firstChild.nodeValue;
	         this.up = xml.getElementsByTagName("up")[0].firstChild.nodeValue;//大气压力单位
	         this.ur = xml.getElementsByTagName("ur")[0].firstChild.nodeValue;//降水量
	         this.id = xml.getElementsByTagName("loc")[0].getAttribute("id");
	         this.dnam = xml.getElementsByTagName("dnam")[0].firstChild.nodeValue;
	         this.tm = xml.getElementsByTagName("tm")[0].firstChild.nodeValue;
	         this.sunr = xml.getElementsByTagName("sunr")[0].firstChild.nodeValue;
	         this.suns = xml.getElementsByTagName("suns")[0].firstChild.nodeValue;
	         
	         //当前气象信息
	         var cc = xml.getElementsByTagName("cc")[0];
	         this.Ctmp = cc.getElementsByTagName("tmp")[0].firstChild.nodeValue;
	         this.Ct = cc.getElementsByTagName("t")[0].firstChild.nodeValue.t();
	         this.Cicon = cc.getElementsByTagName("icon")[0].firstChild.nodeValue;
	         iCurrenIcon = this.Cicon;

	 }


	//------------------
	//获取并载入气象数据
	//------------------
	function fnLoadWeatherData(code){
	         //初始化
	         print("Load weather...");
	         fnCloseMsg();
	         sTemp="";
	         iCurrenCityId=indexof(code);
	         
	         Loadingstart();
	         //Ajax load
           var params = {};  
           params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
           params[gadgets.io.RequestParameters.REFRESH_INTERVAL] = 30;  
	         gadgets.io.makeRequest(
	                          "http://xoap.weather.com/weather/local/"+code+"?cc=*&prod=xoap&link=xoap&par=1017780104&key=7d248cae7bcf8618&unit="+sUnit, 
	                          function (Oxml){
	                                        if (Oxml.errors.length>0) {
	                                                   fnOpenMsg(oPrefs.getMsg((Oxml.errors)[0]));
	                                                   //load stop
	                                                   Loadingend();
	                                                   return;
	                                        }
	                                        print("Load data OK!");																												            
															            fnSetContent(Oxml.data);                                
	                 },params)
	                 var aName=aCityNames[iCurrenCityId].split(", ");
	                 var reInName = /[\s',]/g;
	                 _IG_Analytics("UA-2474779-5", "/code/weathergadget/"+aName[1].replace(reInName,"-")+"/"+aName[0].replace(reInName,"-"));
	 }

	//------------------
	//打开菜单
	//------------------
	function HeadCitysIO(){
	                                  var v = _gel("urlary");
	                                  if(v.innerHTML==""){
	                                  v.style.display = "block";
	                                  v.style.width = _gel("nowc").offsetWidth+"px";
		                                for(i=0;i<aCityNames.length;i++){
	                                      v.innerHTML += "<a id=\""+aCityNames[i]+"\" class=\"urlary\" onclick=\"fnLoadWeatherData('"+aCityCodes[i]+"');return false\">"+aCityNames[i]+"</a><br/>";
	                                  }
	                                  }
	                                  else{
	                                  v.style.display="none";
	                                  v.innerHTML="";
	                                  }
	  }
	 
	//------------------
	//Loading start...
	//------------------
	function Loadingstart(){
	                   var oPanelBox = _gel("navpanel");
	                   var oLoadBox = _gel("loading");
	                   oLoadBox.style.left = oPanelBox.offsetWidth/2-16;
	                   oLoadBox.style.top = oPanelBox.offsetHeight/2-16;
	 }
	
	//------------------
	//Loading end...
	//------------------
	function Loadingend(){
	                   var oLoadBox = _gel("loading");
	                   oLoadBox.style.left = "-200px"
	                   oLoadBox.style.top = "-200px";
	 }

function navigateTocanvas() {
  var supported_views = gadgets.views.getSupportedViews();
  gadgets.views.requestNavigateTo(supported_views["canvas"],{nowId:iCurrenCityId});
}
	 
function init_home(){
	//debug=true;									  
  //载入第一个
  fnLoadWeatherData(aCityCodes[0]);
  $("#citysbutton").click(HeadCitysIO);
  $("#nowcity").click(HeadCitysIO);
  $("#tempicon").click(navigateTocanvas);
  print("Load init");
} 